package com.gs.zsmanage.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: chz
 * @Date: 2018/10/22 11:35
 * @Describe: 时间工具类
 */
public class DateUtil {

    private final static DateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

    private final static DateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");

    private final static DateFormat yyyyMMddHHmm = new SimpleDateFormat("yyyyMMddHHmm");

    private final static DateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取过去某一天的时间
     *
     * @param day
     * @param temp
     * @return
     */
    public static String getLastDay(String day, int temp) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = yyyyMMdd.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        c.setTime(date);
        c.add(Calendar.DATE, temp);
        date = c.getTime();
        day = yyyyMMdd.format(date);
        return day;
    }

    /**
     * 比较时间大小  time1 >= time2 true
     *
     * @param time1
     * @param time2
     * @return
     */
    public static boolean compareTime(String time1, String time2) {
        try {
            Date date1 = yyyyMMddHHmm.parse(time1);
            Date date2 = yyyyMMddHHmm.parse(time2);
            if (date1.getTime() >= date2.getTime()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getTime() {
        return yyyyMMddHHmm.format(new Date());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getTime2() {
        return yyyy_MM_dd_HH_mm_ss.format(new Date());
    }
}
